#!/usr/bin/env python
# encoding: utf-8
'''
@author: Excelsiorly
@license: (C) Copyright 2022, All Rights Reserved.
@contact: excelsiorly@qq.com
@file: 155. 最小栈.py
@time: 2022/1/20 1:20
@desc: https://leetcode-cn.com/problems/min-stack/
> 设计一个支持 push ，pop ，top 操作，并能在常数时间内检索到最小元素的栈。

实现 MinStack 类:
    - MinStack() 初始化堆栈对象。
    - void push(int val) 将元素val推入堆栈。
    - void pop() 删除堆栈顶部的元素。
    - int top() 获取堆栈顶部的元素。
    - int getMin() 获取堆栈中的最小元素。

1. Ot(1), Os(n)
'''

import heapq
class MinStack(object):

    def __init__(self):
        self.stack = []
        self.min_stack = []
        self.min_stack.append(2**31-1)

    def push(self, val):
        """
        :type val: int
        :rtype: None
        """
        self.stack.append(val)
        self.min_stack.append(min(val, self.min_stack[-1]))



    def pop(self):
        """
        :rtype: None
        """
        self.stack.pop()
        self.min_stack.pop()


    def top(self):
        """
        :rtype: int
        """
        return self.stack[-1]


    def getMin(self):
        """
        :rtype: int
        """
        return self.min_stack[-1]



# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()